package com.aauedu.springboot.controller;

import com.aauedu.springboot.bean.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@Controller
public class WelcomeController {




    @GetMapping("/welcome")
    public String welcome(Model model, HttpServletRequest request, Admin admin, HttpSession session) {
        //获取cookies
        //这个cookies可能是nul，如果不是null，数组的长度一定是>0的
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("username".equals(name)) {
                    username = cookie.getValue();
                } else if ("password".equals(name)) {
                    password = cookie.getValue();
                }
            }
        }
        //要在这里使用username和password变量
        if (username != null && password != null) {
            //跳转到列表页面
            //验证用户名和密码是否正确
            //正确，表示登录成功
            //错误，表示登录失败
            //JDBC代码->登录成功
            if (StringUtils.hasText(username) && "123456".equals(password)) {
                session.setAttribute("loginAdmin", admin);
                return "redirect:/monster.html";
            } else {
                //登录失败
                return "adminLogin";
            }
        } else {
            //跳转到登录页面
            model.addAttribute("msg", "用户名/密码错误");
            return "adminLogin";
        }
    }
}
